/*
 * Created on Fri Sep 29 2017
 * @flow
 * Copyright (c) 2017 jianglinjie
 */
'use strict';

import * as GenderTypes from '../constants/GenderTypes';
import * as CaptchaBusiness from '../constants/CaptchaBusiness';

/**
 * 是否是合法的密码
 * 
 * @export
 * @param {string} [pass=''] 
 * @returns {boolean} 
 */
export function checkPass(pass?: string = ''): boolean {
  let regx = /^[a-zA-Z0-9]{6,256}$/; //6到20位 字母和数字组成
  if (!regx.test(pass)) {
    return false;
  }

  return true;
}

/**
 * 是否是合法的用户名
 * 
 * @export
 * @param {string} [pass=''] 
 * @returns {boolean} 
 */
export function checkUserName(userName?: string = ''): boolean {
  let regx = /^[a-zA-Z]{1}([a-zA-Z0-9]|[-]|[_]){5,256}$/; //5到20位 字母和数字组成中划线 下划线 字母开头
  if (!regx.test(userName)) {
    return false;
  }

  return true;
}


/**
 * 是否是电话号码
 * 
 * @export
 * @param {string} [str=''] 
 * @returns {boolean} 
 */
export function isPhone(str?: string = ''): boolean {
  let regx = /^[0-9]{11}$/;
  if (!regx.test(str)) {
    return false;
  }

  return true;
}


export function checkGender(gender?: string = ''): boolean {
  for (let key of Object.keys(GenderTypes)) {
    if (GenderTypes[key] === gender) {
      return true;
    }
  }

  return false;
}

export function checkAvatar(avatar?: string = ''): boolean {
  let regx = /^http/;
  if (!regx.test(avatar)) {
    return false;
  }

  return true;
}

export function checkNickName(nickName?: string = ''): boolean {
  //不能为空 或超过30个字符
  if (!nickName || nickName.length > 30) {
    return false;
  }

  return true;
}

export function checkCaptchaBusiness(business?: string = ''): boolean {
  for (let key of Object.keys(CaptchaBusiness)) {
    if (CaptchaBusiness[key] === business) {
      return true;
    }
  }

  return false;
}